package com.cc.app.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.cc.app.view.TestView;

/**
 * 动画
 */
public class PathAnimateView extends TestView {

    private Path innerCircle;
    private Path outerCircle;

    private Path triangle1;
    private Path triangle2;

    private Path drawPath = new Path();

    private PathMeasure pathMeasure;

    private int duration = 2000;

    private float ratio;

    private State state = State.CIRCLE;

    private Paint paint;

    private ValueAnimator animator;

    private Handler handler;

    private RectF innerRect = new RectF(-220, -220, 220, 220);
    private RectF outerRect = new RectF(-280, -280, 280, 280);

    private enum State {
        CIRCLE, TRANGLE, END
    }

    public PathAnimateView(Context context) {
        this(context, null);
    }

    public PathAnimateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        initPaint();
        initPath();
        initHandler();
        initListener();
        initAnimator();
    }

    private void initAnimator() {
        animator = ValueAnimator.ofFloat(0, 1).setDuration(duration);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ratio = (float) animation.getAnimatedValue();
                invalidate();
            }
        });

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation, boolean isReverse) {
                handler.sendEmptyMessage(0);
            }
        });
    }

    private void initListener() {
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                state = State.CIRCLE;
                animator.start();
            }
        });
    }

    private void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (state) {
                    case CIRCLE:
                        state = State.TRANGLE;
                        animator.start();
                        break;
                    case TRANGLE:
                        state = State.END;
                        animator.start();
                        break;
                }
            }
        };
    }

    private void initPath() {
        innerCircle = new Path();
        outerCircle = new Path();
        triangle1 = new Path();
        triangle2 = new Path();
        drawPath = new Path();

        innerCircle.addArc(innerRect, 150, -359.9f);
        outerCircle.addArc(outerRect, 60, -359.9f);

        pathMeasure = new PathMeasure();
        pathMeasure.setPath(innerCircle, false);

        float[] pos = new float[2];
        pathMeasure.getPosTan(0, pos, null);

        triangle1.moveTo(pos[0], pos[1]);

        pathMeasure.getPosTan(1f / 3f * pathMeasure.getLength(), pos, null);
        triangle1.lineTo(pos[0], pos[1]);

        pathMeasure.getPosTan(2f / 3f * pathMeasure.getLength(), pos, null);
        triangle1.lineTo(pos[0], pos[1]);
        triangle1.close();

        Matrix matrix = new Matrix();
        matrix.postRotate(-180);
        triangle1.transform(matrix, triangle2);
    }

    private void initPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeJoin(Paint.Join.BEVEL);
        paint.setShadowLayer(15, 0, 0, Color.YELLOW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.translate(getWidth() / 2, getHeight() / 2);

        draw1(canvas);
        canvas.restore();
    }

    /**
     * 动画轨迹
     *
     * @param canvas
     */
    private void draw1(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        switch (state) {
            case CIRCLE:
                drawPath.reset();
                pathMeasure.setPath(innerCircle, false);
                pathMeasure.getSegment(0, ratio * pathMeasure.getLength(), drawPath, true);
                canvas.drawPath(drawPath, paint);

                drawPath.reset();
                pathMeasure.setPath(outerCircle, false);
                pathMeasure.getSegment(0, ratio * pathMeasure.getLength(), drawPath, true);
                canvas.drawPath(drawPath, paint);

                break;

            case TRANGLE:
                canvas.drawPath(innerCircle, paint);
                canvas.drawPath(outerCircle, paint);

                drawPath.reset();
                pathMeasure.setPath(triangle1, false);

                float stopD = ratio * pathMeasure.getLength();
                float startD = stopD - (0.5f - Math.abs(0.5f - ratio)) * 200;

                pathMeasure.getSegment(startD, stopD, drawPath, true);
                canvas.drawPath(drawPath, paint);

                drawPath.reset();
                pathMeasure.setPath(triangle2, false);
                pathMeasure.getSegment(startD, stopD, drawPath, true);
                canvas.drawPath(drawPath, paint);
                break;

            case END:
                canvas.drawPath(innerCircle, paint);
                canvas.drawPath(outerCircle, paint);

                drawPath.reset();
                pathMeasure.setPath(triangle1, false);
                pathMeasure.getSegment(0, ratio * pathMeasure.getLength(), drawPath, true);
                canvas.drawPath(drawPath, paint);

                drawPath.reset();
                pathMeasure.setPath(triangle2, false);
                pathMeasure.getSegment(0, ratio * pathMeasure.getLength(), drawPath, true);
                canvas.drawPath(drawPath, paint);
                break;
        }
    }
}
